<template>
  <div>
    <el-form label-position="left" label-width="100px" style="width:100%;">
      <el-form-item label="用户ID：">
        <span v-if="id">{{ formData.userId }}</span>
        <el-input v-if="!id" v-model="formData.userId" placeholder="请输入用户ID" class="filter-item" />
      </el-form-item>
      <el-form-item label="用户名："><el-input v-model="formData.userName" placeholder="请输入用户名" class="filter-item" /></el-form-item>
      <el-form-item label="限权：">
        <el-select v-model="formData.roleId" placeholder="请选择限权" clearable class="filter-item">
          <el-option v-for="item in sysRoles" :key="item.id" :label="item.roleName" :value="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="cancel">取消</el-button>
      <el-button v-waves :loading="confirmLoading" type="primary" icon="el-icon-check" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getSysUserDetail, addSysUser, editSysUser } from '@/api/user';
import { getSysRole } from '@/api/role';
import waves from '@/directive/waves';

export default {
  name: 'SysUserAdd',
  directives: { waves },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
      },
      sysRoles: []
    };
  },
  created() {
    this.getSysRole();
    if (this.id) {
      this.getDetail(this.id);
    }
  },
  methods: {
    async getDetail(id) {
      await getSysUserDetail(id).then(response => {
        this.formData = response.data;
      });
    },
    async getSysRole() {
      await getSysRole({
        pageNum: 1,
        pageSize: 9999
      }).then(response => {
        this.sysRoles = response.data.list;
      });
    },
    async submit() {
      if (this.id) {
        if (this.formData.userId === '') {
          this.$message.error('请输入用户ID');
          return;
        }
      }
      if (this.formData.userName === '') {
        this.$message.error('请输入用户名');
        return;
      }
      if (this.formData.roleId === null) {
        this.$message.error('请选择权限');
        return;
      }
      this.confirmLoading = true;
      if (this.id) {
        await editSysUser(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('编辑成功');
          this.$emit('cancelClose', true, 'edit');
        });
      } else {
        await addSysUser(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('新增成功');
          this.$emit('cancelClose', true, 'add');
        });
      }
    },
    cancel() {
      if (this.id) {
        this.$emit('cancelClose', false, 'edit');
      } else {
        this.$emit('cancelClose', false, 'add');
      }
    }
  }
};
</script>
